<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="misc.Configurations"%>
<%@page import="controller.DeleteController,controller.FileDeletionController"%>
<%@page import="data.File"%>
<%@page import="data.AuthorizationSession"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title><%=Configurations.title%>Delete the file</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript">
function toggle(){
    if(document.getElementById('choice').checked)
        document.getElementById('deleteBtn').disabled=false;
    else
        document.getElementById('deleteBtn').disabled=true;
}
</script>
</head>
<body>
<h1>Delete this file</h1>
<%
AuthorizationSession.checkAuthorizationToken(request,response,true);

if(request.getParameter("file")!=null)
{
    DeleteController control = new DeleteController();
    File this_file = control.retrieveFile(request.getParameter("file"));
    
    if(this_file!=null)
    {
        if(!this_file.isDeleted())
        {
            if(request.getParameter("choice")!=null)
            {
                
                    FileDeletionController filecontrol = new FileDeletionController();
                    if(filecontrol.purgeFileNow(this_file.getFileID())){
                %>
                <div class="success">The file is deleted successfully!</div>
                <%
                    }
                    else
                    {
                        %>
                        <div class="success">Someone is downloading this file. The file will be automatically deleted when the downlading is completed.</div>		
                        <%	
                    }
            }
            else
            {
            %>
            <form action="Delete.jsp?file=<%= this_file.getHashedCode() %>" method="post">
                <table border="0">
                    <tr>
                      <td>File name:</td><td><%= this_file.getName() %></td>
                    </tr>
                    <tr>
                      <td>File size:</td><td><%= File.convertToMB(this_file.getSize()) %></td>
                    </tr>
                    <tr>
                      <td colspan="2" align="right">Are you sure? <input type="checkbox" id="choice" name="choice" value="yes" onclick="javascript:toggle();"/></td>
                    </tr>
                    <tr>
                      <td colspan="2" align="right"><input type="submit" value="Delete" id="deleteBtn" disabled="disabled"/></td>
                    </tr>
                </table>
            </form>
            <%
            }
        }
        else
        {
            %>
            <div class="error">The file is deleted already!</div>
            <%
        }
    }
    else
    {
        %>
        <div class="error">The file doesn't exist!</div>
        <%
    }
}
%>
<form>
    <input type="button" value="Back to management" onclick="javascript:window.location = 'ManageFiles.jsp';"/>
</form>
</body>
</html>